package com.botao.cms.service.impl;

import com.botao.cms.dao.UserMapper;
import com.botao.cms.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Random;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommonServiceImpl implements CommonService {
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    UserMapper userMapper;
    @Async
    @Override
    public void sendMailCheck(HttpSession session, String email) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 6; i++) {
            sb.append(random.nextInt(10));
        }
        session.setAttribute("registerYzm", sb.toString());
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("2867970722@qq.com");
        simpleMailMessage.setSubject("星时代注册验证");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setText("尊敬的用户你好，你正在注册星时代管理系统后台账号，你的验证码是：" + sb + "，请尽快完成验证！");
        javaMailSender.send(simpleMailMessage);
    }
}
